<?php

/**
 * 图片选择器
 */

namespace generate\field;

class ImageManage extends Field
{
    public static $html = <<<EOF
<div class="layui-form-item">
    <label for="[FIELD_NAME]" class="layui-form-label">[FORM_NAME]</label>
    <div class="layui-input-inline">
        <button type="button" class="layui-btn" id="[FIELD_NAME]">
            <i class="layui-icon"></i>
            选择图片
        </button>
        <div class="layui_img">
            <img src="{\$data.[FIELD_NAME]|default=''}" {if !isset(\$data.[FIELD_NAME]) || \$data.[FIELD_NAME]=='' }
                style="display: none;" {/if} id="srcimg[FIELD_NAME]" alt="" class="srcimgavatar"
                srcset="">
            <input type="hidden" name="[FIELD_NAME]" id="inputimg[FIELD_NAME]"
                value="{\$data.[FIELD_NAME]|default=''}">
        </div>
    </div>

</div>
<script>
    $('#[FIELD_NAME]').click(function () {
        layer.open({
            type: 2
            , area: ['80%', '80%']
            , content: "{:url('filemanage/manage')}"
            ,shadeClose :true
        });
        window.manage = {
            callback: function (res) {
                $("#inputimg[FIELD_NAME]").val(res.thumb);
                $("#srcimg[FIELD_NAME]").attr('src', res.thumb).show();
            }
        }

    })

</script>\n 
EOF;

    public static $rules = [
        'file_size'  => '文件大小限制',
        'file_image' => '图片类型',
        'regular'    => '自定义正则'
    ];

    public static function create($data)
    {
        $html = self::$html;
        $html = str_replace(array('[FORM_NAME]', '[FIELD_NAME]'), array($data['form_name'], $data['field_name']), $html);
        return $html;
    }
}
